<?php
namespace Home\Controller;
use Think\Controller;
class WarehouseController extends BackController {
	protected $limitnum = 10;
	public function index(){
		echo 111;
		return 222;
	}


	//库存列表
	public function warehousing(){
		//高级搜索
        $keyword = I('keyword');
        
        //分页
        $result['limit'] = I('p');
        //条形码
        $result['barcode'] = I('quiz1');
        if ($result['barcode']){
            $where['barcode'] = $result['barcode'];
        }
        $where = array();
        $where['opt'] = 1;
        //商品分类
        $result['class'] = I('quiz2');
        if ($result['class']){
            $where['class'] = $result['class'];
        }
         //商品分类
        $result['goodsname'] = I('quiz3');
        if ($result['goodsname']){
            $where['goodsname'] = $result['goodsname'];
        }
        //供应商
        $result['supplier'] = I('quiz4');
        if ($result['supplier']){
            $where['supplier'] = $result['supplier'];
        }
         //仓库
        $result['warehouse'] = I('quiz5');
        if ($result['warehouse']){
            $where['warehouse'] = $result['warehouse'];
        }
        //入库日期
        $result['logtime'] = I('enddate');
        if ($result['logtime']){
            $where['logtime'] = strtotime($result['logtime']);
        }

	    if ($keyword){
	        $where['barcode|goodsname|supplier'] = array('like','%'.$keyword.'%');
        }
	    if ($result['limit'] <= 0 || empty($result['limit'])){
            $limit['limit'] = 1;
        }else{
            $limit['limit'] = $result['limit'];
            // var_dump($limit['limit']);exit;
        }
		//var_dump($about)
		
		// var_dump($userList);exit;
		


		$end = $limit['limit']*$this->limitnum;
        $star = ($limit['limit']-1)*$this->limitnum;
        $userList = M('management')->where($where)->limit($star,$end)->select();
        foreach ($userList as $k => $v) {
			$userList[$k]['sum'] = $v['purchaseprice']*$v['inventory'];
			foreach ($v as $ke => $va) {
				$userList[$k]['logtime'] = date("Y-m-d H:i:s",$v['logtime']);
			}
		}
		$count = M('management')->where($where)->count();
		$Page = new \Think\Page($count, $this->limitnum);// 实例化分页类 传入总记录数和每页显示的记录数
        $Page->setConfig('theme', " <ul class='pagination pagination-large'></li><li>%FIRST%</li><li>%UP_PAGE%</li><li>%LINK_PAGE%</li><li>%DOWN_PAGE%</li><li>%END%</li><li><a> %HEADER%  %NOW_PAGE%/%TOTAL_PAGE% 页</a></ul>");//(对thinkphp自带分页的格式进行自定义)
        $show = $Page->show(); // 分页显示输出
        $this->assign("count", $count);
        $this->assign("keyword", $keyword);
        $this->assign("page", $show);
		$this->assign('userList',$userList);
		$this->display();
	}


	//添加入库信息
	public function addhous(){
		if(IS_POST){
			$data = I('post.');
			$data['logtime'] = strtotime($data['enddate']);
			$post = M('management')->add($data);
			if($post){
                $this->success('添加成功',U('Warehouse/warehousing'));exit;
            }else{
                $this->error('添加失败',U('Warehouse/warehousing'));exit;
            }
		}
		var_dump('fail');	
		return 'fail';
		$this->display();
	}


	//删除入库信息
	public function delhous(){
		$id = I('get.id');
		$res = M('management')->where('id='.$id)->delete();
		if($res){
            $this->success('删除成功',U('Warehouse/warehousing'));exit;
        }else{
            $this->error('删除失败',U('Warehouse/warehousing'));exit;
        }
	}


	//修改入库信息
	public function edithous(){
		if(IS_POST){
			$data = I('post.');
			$id = $data['id'];
			// unset();
			$data['logtime'] = strtotime($data['logtime']);
			$post = M('management')->where('id='.$id)->save($data);
			if($post){
                $this->success('修改成功',U('Warehouse/warehousing'));exit;
            }else{
                $this->error('修改失败',U('Warehouse/warehousing'));exit;
            }
		}

		$id = I('get.id');
		$list = M('management')->where('id='.$id)->find();
		$list['logtime'] = date("Y-m-d",$list['logtime']);
		$this->assign('list',$list);
		$this->display();
	}



	//库存列表
	public function stock(){


		//高级搜索
        $keyword = I('keyword');
        
        //分页
        $result['limit'] = I('p');
        //条形码
        $result['barcode'] = I('quiz1');
        if ($result['barcode']){
            $where['barcode'] = $result['barcode'];
        }
        $where = array();
        $where['opt'] = 0;
        //商品分类
        $result['class'] = I('quiz2');
        if ($result['class']){
            $where['class'] = $result['class'];
        }
         //商品分类
        $result['goodsname'] = I('quiz3');
        if ($result['goodsname']){
            $where['goodsname'] = $result['goodsname'];
        }
        //供应商
        $result['supplier'] = I('quiz4');
        if ($result['supplier']){
            $where['supplier'] = $result['supplier'];
        }
         //仓库
        $result['warehouse'] = I('quiz5');
        if ($result['warehouse']){
            $where['warehouse'] = $result['warehouse'];
        }
        //入库日期
        $result['logtime'] = I('enddate');
        if ($result['logtime']){
            $where['logtime'] = strtotime($result['logtime']);
        }

	    if ($keyword){
	        $where['barcode|goodsname|serial'] = array('like','%'.$keyword.'%');
        }
	    if ($result['limit'] <= 0 || empty($result['limit'])){
            $limit['limit'] = 1;
        }else{
            $limit['limit'] = $result['limit'];
            // var_dump($limit['limit']);exit;
        }


		$end = $limit['limit']*$this->limitnum;
        $star = ($limit['limit']-1)*$this->limitnum;
        $userList = M('management')->where($where)->limit($star,$end)->select();
        foreach ($userList as $k => $v) {
			foreach ($v as $ke => $va) {
				$userList[$k]['logtime'] = date("Y-m-d H:i:s",$v['logtime']);
			}
		}
		$count = M('management')->where($where)->count();
		$Page = new \Think\Page($count, $this->limitnum);// 实例化分页类 传入总记录数和每页显示的记录数
        $Page->setConfig('theme', " <ul class='pagination pagination-large'></li><li>%FIRST%</li><li>%UP_PAGE%</li><li>%LINK_PAGE%</li><li>%DOWN_PAGE%</li><li>%END%</li><li><a> %HEADER%  %NOW_PAGE%/%TOTAL_PAGE% 页</a></ul>");//(对thinkphp自带分页的格式进行自定义)
        $show = $Page->show(); // 分页显示输出
        $this->assign("count", $count);
        $this->assign("keyword", $keyword);
        $this->assign("page", $show);
		$this->assign('userList',$userList);
		$this->display();
		
	}


	//修改库存信息
	public function editStock(){
		if(IS_POST){
			$data = I('post.');
			$id = $data['id'];
			$post = M('management')->where('id='.$id)->save($data);
			if($post){
                $this->success('修改成功',U('Warehouse/stock'));exit;
            }else{
                $this->error('修改失败',U('Warehouse/stock'));exit;
            }
		}

		$id = I('get.id');
		if($id){
			$list = M('management')->where('id='.$id)->find();
			$list['logtime'] = date("Y-m-d",$list['logtime']);
		}else{
			$list = array();
		}
		
		$this->assign('list',$list);
		$this->display();
		
	}



	//删除入库信息
	public function delStock(){
		$id = I('get.id');
		$res = M('management')->where('id='.$id)->delete();
		if($res){
            $this->success('删除成功',U('Warehouse/stock'));exit;
        }else{
            $this->error('删除失败',U('Warehouse/stock'));exit;
        }
	}


	/**
	   * 出库列表
	   */
	public function outgoing(){
        
        //分页
        $result['limit'] = I('p');
        //条形码
        $result['recipient'] = I('recipient');
        if ($result['recipient']){
            $where['recipient'] = $result['recipient'];
        }
        $where = array();
        $where['opt'] = 2;
        //商品分类
        $result['class'] = I('class');
        if ($result['class']){
            $where['class'] = $result['class'];
        }
         //商品分类
        $result['goodsname'] = I('goodsname');
        if ($result['goodsname']){
            $where['goodsname'] = $result['goodsname'];
            // var_dump($where);exit;
        }
        //供应商
        $result['supplier'] = I('supplier');
        if ($result['supplier']){
            $where['supplier'] = $result['supplier'];
        }
         //仓库
        $result['warehouse'] = I('warehouse');
        if ($result['warehouse']){
            $where['warehouse'] = $result['warehouse'];
        }
          //仓库
        $result['status'] = I('status');
        if ($result['status']){
            $where['status'] = $result['status']-1;
            // var_dump($where);exit;
        }
        //入库日期
        $result['logtime'] = I('enddate');
        if ($result['logtime']){
            $where['logtime'] = strtotime($result['logtime']);
        }

	    if ($result['limit'] <= 0 || empty($result['limit'])){
            $limit['limit'] = 1;
        }else{
            $limit['limit'] = $result['limit'];
            // var_dump($limit['limit']);exit;
        }


		$end = $limit['limit']*$this->limitnum;
        $star = ($limit['limit']-1)*$this->limitnum;
        $userList = M('management')->where($where)->limit($star,$end)->select();
        foreach ($userList as $k => $v) {
			foreach ($v as $ke => $va) {
				$userList[$k]['logtime'] = date("Y-m-d H:i:s",$v['logtime']);
			}
		}
		$count = M('management')->where($where)->count();
		$Page = new \Think\Page($count, $this->limitnum);// 实例化分页类 传入总记录数和每页显示的记录数
        $Page->setConfig('theme', " <ul class='pagination pagination-large'></li><li>%FIRST%</li><li>%UP_PAGE%</li><li>%LINK_PAGE%</li><li>%DOWN_PAGE%</li><li>%END%</li><li><a> %HEADER%  %NOW_PAGE%/%TOTAL_PAGE% 页</a></ul>");//(对thinkphp自带分页的格式进行自定义)
        $show = $Page->show(); // 分页显示输出
        $this->assign("count", $count);
        $this->assign("keyword", $keyword);
        $this->assign("page", $show);
		$this->assign('userList',$userList);
		$this->display();
	}


	//修改入库信息
	public function editOutgoing(){
		if(IS_POST){
			$data = I('post.');
			$id = $data['id'];
			$post = M('management')->where('id='.$id)->save($data);
			if($post){
                $this->success('修改成功',U('Warehouse/outgoing'));exit;
            }else{
                $this->error('修改失败',U('Warehouse/outgoing'));exit;
            }
		}

		$id = I('get.id');
		if($id){
			$list = M('management')->where('id='.$id)->find();
			$list['logtime'] = date("Y-m-d",$list['logtime']);
		}else{
			$list = '';
		}
		
		$this->assign('list',$list);
		$this->display();
		
	}


	//删除入库信息
	public function delOutgoing(){
		$id = I('get.id');
		$res = M('management')->where('id='.$id)->delete();
		if($res){
            $this->success('删除成功',U('Warehouse/outgoing'));exit;
        }else{
            $this->error('删除失败',U('Warehouse/outgoing'));exit;
        }
	}


	/**
	   * 盘点列表
	   */
	public function Check(){
		//分页
        $result['limit'] = I('p');
        //条形码
        $result['barcode'] = I('barcode');
        if ($result['barcode']){
            $where['barcode'] = $result['barcode'];
        }
        $where = array();
        $where['opt'] = 3;
        //商品分类
        $result['class'] = I('class');
        if ($result['class']){
            $where['class'] = $result['class'];
        }
         //商品名称
        $result['goodsname'] = I('goodsname');
        if ($result['goodsname']){
            $where['goodsname'] = $result['goodsname'];
            // var_dump($where);exit;
        }
        //录入员
        $result['supplier'] = I('supplier');
        if ($result['supplier']){
            $where['supplier'] = $result['supplier'];
        }
        //仓库
        $result['warehouse'] = I('warehouse');
        if ($result['warehouse']){
            $where['warehouse'] = $result['warehouse'];
        }
          //损溢数量
        $result['inventory'] = I('inventory');
        if ($result['inventory']){
            $where['inventory'] = $result['inventory'];
            // var_dump($where);exit;
        }

         //损溢类型
        $result['profittype'] = I('profittype');
        if ($result['profittype']){
            $where['profittype'] = $result['profittype'];
            // var_dump($where);exit;
        }
        //损溢日期
        $result['logtime'] = I('enddate');
        if ($result['logtime']){
            $where['logtime'] = strtotime($result['logtime']);
        }

	    if ($result['limit'] <= 0 || empty($result['limit'])){
            $limit['limit'] = 1;
        }else{
            $limit['limit'] = $result['limit'];
        }


		$end = $limit['limit']*$this->limitnum;
        $star = ($limit['limit']-1)*$this->limitnum;
        $userList = M('management')->where($where)->limit($star,$end)->select();
        foreach ($userList as $k => $v) {
			foreach ($v as $ke => $va) {
				$userList[$k]['logtime'] = date("Y-m-d H:i:s",$v['logtime']);
			}
		}
		$count = M('management')->where($where)->count();
		$Page = new \Think\Page($count, $this->limitnum);// 实例化分页类 传入总记录数和每页显示的记录数
        $Page->setConfig('theme', " <ul class='pagination pagination-large'></li><li>%FIRST%</li><li>%UP_PAGE%</li><li>%LINK_PAGE%</li><li>%DOWN_PAGE%</li><li>%END%</li><li><a> %HEADER%  %NOW_PAGE%/%TOTAL_PAGE% 页</a></ul>");//(对thinkphp自带分页的格式进行自定义)
        $show = $Page->show(); // 分页显示输出
        $this->assign("count", $count);
        $this->assign("keyword", $keyword);
        $this->assign("page", $show);
		$this->assign('userList',$userList);
		$this->display();
	}





	/**
	   * 修改盘点列表
	   */
	public function editCheck(){
		if(IS_POST){
			$data = I('post.');
			$id = $data['id'];
			$data['logtime'] = strtotime($data['logtime']);
			$post = M('management')->where('id='.$id)->save($data);
			if($post){
                $this->success('修改成功',U('Warehouse/Check'));exit;
            }else{
                $this->error('修改失败',U('Warehouse/Check'));exit;
            }
		}

		$id = I('get.id');
		if($id){
			$list = M('management')->where('id='.$id)->find();
			$list['logtime'] = date("Y-m-d",$list['logtime']);
		}else{
			$list = '';
		}
		
		$this->assign('list',$list);
		$this->display();
		
	}



	/**
	   * 盘点列表
	   */
	//删除盘点信息
	public function delcheck(){
		$id = I('get.id');
		$res = M('management')->where('id='.$id)->delete();
		if($res){
            $this->success('删除成功',U('Warehouse/Check'));exit;
        }else{
            $this->error('删除失败',U('Warehouse/Check'));exit;
        }
	}





//上面的是我这边做的功能；不要改动！谢谢！<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<


    /**
     * 商品分类列表
     */
    public function goodsCassify()
    {
        //使用D调用视图模型
        $data = D("good_type")->order('sort asc')->select();
        $this->assign('data',$data);
        $this->display();
    }

    /**
     * 新增商品分类
     */
    public function goodsAdd()
    {
        if (IS_POST) {
            $data['name'] = I('post.name');
            $data['company'] = I('post.company');
            $data['state'] = I('post.state');
            //通过D实例化模型类
            $user =  D("good_type");
            if ($user->create()) {
                if ($user->add($data)) {
                    $this->success('新增成功',U('Warehouse/goodsCassify'));
                } else {
                    $this->error('新增失败',U('Warehouse/goodsCassify'));
                }
            } else {
                $this->error($user->getError());
            }
        }
    }

    /**
     * 更新商品分类
     */
    public function goodsedit()
    {
        if (IS_POST) {
            if (I('post.id')) {
                $id = I('post.id');
            }
            if (I("post.name")) {
                $data['name'] = I("post.name");
            }
            if (I("post.company")) {
                $data['company'] = I("post.company");
            }
            $data['state'] = I("post.quizl");
            if (I("post.sort")) {
                $data['sort'] = I("post.sort");
            }
            $user = M("good_type");
            if ($user->create()) {
                if ($user->where('id='.$id)->save($data) !== false) {
                    echo 'Success';
                } else {
                    echo 'Error';
                }
            } else {
                $this->error($user->getError());
            }
        } else {
            echo "Error2";
        }
    }

    /**
     * 删除商品分类
     */
    public function del() {
        $id = I('id');
        $user =  D("good_type");
        $row = $user->where('id='.$id)->delete();
        if ($row) {
            $this->success('删除成功',U('Warehouse/goodsCassify'));
        } else {
            $this->error('删除失败',U('Warehouse/goodsCassify'));
        }

    }

    /**
     * 仓库设置列表
     */
    public function warehouseSetup()
    {
        //使用D调用视图模型
        $user = D('warehouse_setting');
        $data = $user->order('sort asc')->select();
        $school = M('school_area')->select();
        $this->assign('data',$data);
        $this->assign('school',$school);
        $this->display();
    }

    /**
     * 仓库设置添加
     */
    public function warehouseSetupadd()
    {
        if(IS_POST) {
            $data['name'] = I('post.name');
            $data['mobile'] = I('post.mobile');
            $data['address'] = I('post.address');
            $data['xs_state'] = I('post.xs_state');
            $data['kq_state'] = I('post.kq_state');
            $data['school_id'] = I('post.quiz1');
            //通过D实例化模型类
            $user =  D("warehouse_setting");
            if ($user->create()) {
                if ($user->add($data)) {
                    $this->success('新增成功',U('Warehouse/warehouseSetup'));
                } else {
                    $this->error('新增失败',U('Warehouse/warehouseSetup'));
                }
            } else {
                $this->error($user->getError());
            }
        }
    }


    /**
     *  仓库设置更新
     */
    public function warehouseSetupedit()
    {
        if (IS_POST) {
            if (I('post.id')) {
                $id = I('post.id');
            }
            if (I("post.name")) {
                $data['name'] = I("post.name");
            }
            if (I("post.address")) {
                $data['address'] = I("post.address");
            }
            if (I("post.quizl")) {
                $data['xs_state'] = I("post.quizl");
            }
            $data['kq_state'] = I("post.quizl2");
            if (I("post.quizl3")) {
                $data['school_id'] = I("post.quizl3");
            }
            if (I("post.mobile")) {
                $data['mobile'] = I("post.mobile");
            }
            if (I("post.sort")) {
                $data['sort'] = I("post.sort");
            }
            $user = M("warehouse_setting");
            if ($user->create()) {
                if ($user->where('id='.$id)->save($data) !== false) {
                    echo 'Success';
                } else {
                    echo 'Error';
                }
            } else {
                $this->error($user->getError());
            }
        } else {
            echo 'Error';
        }
    }


    /**
     * 仓库删除
     */
    public function ckdel() {
        $id = I('id');
        $user =  D("warehouse_setting");
        $row = $user->where('id='.$id)->delete();
        if ($row) {
            $this->success('删除成功',U('Warehouse/warehouseSetup'));
        } else {
            $this->error('删除失败',U('Warehouse/warehouseSetup'));
        }

    }
}